package com.ejiaoyi.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ejiaoyi.api.mapper.ApiAuthMapper;
import com.ejiaoyi.api.service.IApiAuthService;
import com.ejiaoyi.common.constant.CacheName;
import com.ejiaoyi.common.entity.ApiAuth;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * API接口认证信息 服务实现类
 * </p>
 *
 * @author unknownChivalrous
 * @since 2020-04-01
 */
@Service
public class ApiAuthServiceImpl extends ServiceImpl<ApiAuthMapper, ApiAuth> implements IApiAuthService {

    @Autowired
    private ApiAuthMapper apiAuthMapper;

    @Override
    @CachePut(value = CacheName.API_REDIS_CACHE, key = "#apiName+'-'+#platform+'-'+#apiKey")
    public boolean authentication(String apiName, String platform, String apiKey) {
        if (StringUtils.isEmpty(apiName) || StringUtils.isEmpty(platform) || StringUtils.isEmpty(apiKey)) {
            return false;
        }

        QueryWrapper<ApiAuth> apiAuthQueryWrapper = new QueryWrapper<ApiAuth>()
                .eq("API_NAME", apiName)
                .eq("PLATFORM", platform)
                .eq("API_KEY", apiKey);

        return apiAuthMapper.selectOne(apiAuthQueryWrapper) != null;
    }
}
